package com.evolve.autotest;

import java.util.List;

import org.apache.log4j.Logger;


public class TestRunner {

	private static final Logger logger = Logger.getLogger(TestRunner.class.getName());
	
	/**
	 * Run all test cases in the test collection
	 * @param testSuiteCollection
	 */
	public void runTestSuites(List<TestSuite> testSuiteCollection) {
		logger.info("Running test cases");
		
		int numTests = 0;
		// Execute tests
		for (TestSuite suite : testSuiteCollection) {
			numTests += suite.getTests().size();
			ReturnValue suiteReturnValue = execute(suite.getTests());
			suite.setReturnValue(suiteReturnValue);
		}
		
		logger.info("Finished running " + testSuiteCollection.size() + " test suites with totally " + numTests + " test cases");
	}
	
	/**
	 * Executes all tests in the suite
	 * @param tests
	 * @return
	 */
	private ReturnValue execute(List<Test> tests) {
		
		ReturnValue suiteReturnValue = ReturnValue.OK;
		
		ReturnValue testReturnValue = ReturnValue.OK;
		
		for (Test test : tests) {
			
			testReturnValue = test.execute();
			
			if (testReturnValue.isMoreSevere(suiteReturnValue)) {
				suiteReturnValue = testReturnValue;
			}
		}
		
		return suiteReturnValue;
	}
}
